package com.baidu.foody.bindingadapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.baidu.foody.R
import com.baidu.foody.viewmodel.MainViewModel
import com.bumptech.glide.Glide
import org.jsoup.Jsoup

object RecipeBindingAdapters {
    @BindingAdapter("shouldShow")
    @JvmStatic
    fun View.shouldShow(viewModel: MainViewModel){
        val available = viewModel.checkNetworkConnection()
        if (available){
            visibility = View.INVISIBLE
        }
    }

    @BindingAdapter("loadImageFromUrl")
    @JvmStatic
    fun ImageView.loadImageFromUrl(url: String){
        Glide.with(context)
            .load(url)
            .placeholder(R.drawable.ic_launcher_background)
            .into(this)
    }

    @BindingAdapter("parseHtml")
    @JvmStatic
    fun TextView.parseHtml(html: String){
        val string = Jsoup.parse(html).text()
        text = string
    }

    @BindingAdapter("setLikeColor")
    @JvmStatic
    fun TextView.setLikeColor(likes: Int){
        if (likes == 0){
            setTextColor(context.resources.getColor(R.color.lightMediumGray,null))
        }else{
            setTextColor(context.resources.getColor(R.color.red,null))
        }
    }

    @BindingAdapter("setVeganColor")
    @JvmStatic
    fun TextView.setVeganColor(isVegan: Boolean){
        if (isVegan){
            setTextColor(context.resources.getColor(R.color.green,null))
        }else{
            setTextColor(context.resources.getColor(R.color.lightMediumGray,null))
        }
    }
}